package com.ruoyi.dm.component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.constant.CollectFileTypeConstants;
import com.ruoyi.common.utils.biz.BizModule;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.domain.po.AirCollectRecordPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.time.LocalDate;

/**
 * 天基 大气风场 graceA
 */
@Component
@Slf4j
public class WindGraceADataStrategy implements DataStrategy<AirCollectRecordPO> {
    @Override
    public AirCollectRecordPO makeupPo(AirCollectRecordPO po) {
        //STANDARD_XX_WND_ACC_2002_09_v02b.txt
        String name = po.getFileName();
        String[] str = name.split("_");
        Integer year = Integer.valueOf(str[4]);
        Integer month = Integer.valueOf(str[5]);
        LocalDate localDate = LocalDate.of(year, month, 1);
        po.setFileDate(localDate);
        return po;
    }

    @Override
    public String pythonCmd(String satellite, String dataType, String filePath, String method, Integer year) {
        String fileType = CollectFileTypeConstants.TYPE_WIND;
        if (!StringUtils.hasLength(method)) {
            //赋予默认值
            method = "linear";
        }
        //只有一个算法
        String outPath = PathUtil.outputModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result");
        String logPath = PathUtil.tmpModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") ;
        //创建文件夹 若无
        FileUtil.mkdir(outPath);
        FileUtil.mkdir(logPath);
        String[] cmdLines = new String[8];
        //python Nimbus7_analysis.py --filepath= --outfile= --outlog=
        String script = PathUtil.codeModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("graceScript");
        cmdLines = new String[10];
        cmdLines[0] = "python";
        cmdLines[1] = script;
        cmdLines[2] = "--filepath";
        cmdLines[3] = filePath;
        cmdLines[4] = "--outfile";
        cmdLines[5] = outPath;
        cmdLines[6] = "--outlog";
        cmdLines[7] = logPath;
        cmdLines[8] = "--inter_type";
        cmdLines[9] = method;
        String commond = ArrayUtil.join(cmdLines, " ");
        log.info("拼接脚本:{}", commond);
        return commond;
    }
}
